/*
 * @Description:
 * @Author: Zzt
 * @Date: 2022-11-10 16:49:18
 * @LastEditTime: 2022-12-08 10:48:52
 */
import { createContext, useReducer, useEffect, useContext } from 'react';
import {
  SET_AT_LIST,
  SET_PERIOD_LIST,
  SET_STORAGE_LIST,
  SET_FILE_TYPE,
  SET_STORAGE_FORMAT,
  SET_PERIOD,
  SET_CATEGORY,
  SET_PERMISS,
  SET_UNIT,
  SET_TIME,
  SET_SECRET
} from '../constants';
import {
  volumeByType,
  countsByFormat,
  volumeByPeriod,
  volumeByCategory,
  volumeBySecret,
  volumeByTime,
  volumeByUnit,
  volumeByPermiss
} from '@/api/statistics/archiveTypeStatistics';
import { FamilyStatisticsReducer } from '../reducers/FamilyStatisticsReducer';
const initialState = {
  /* 档案类型格式列表 */
  fileTypeList: [],
  /* 存储格式 */
  storageFormatList: [],
  /* 保管期限 */
  retentionPeriodList: [],
  categoryList: [],
  permissList: [],
  unitList: [{ totalCapacity: 1 }],
  timeList: [],
  secretList: [],
  ATList: [],
  periodList: [],
  storageList: []
};
const FamilyStatisticsContext = createContext();
const FamilyStatisticsContextProvider = (props) => {
  const [state, dispatch] = useReducer(FamilyStatisticsReducer, initialState);
  const actions = {
    setATList(data) {
      dispatch({ type: SET_AT_LIST, data });
    },
    setPeriodList(data) {
      dispatch({ type: SET_PERIOD_LIST, data });
    },
    setStorageList(data) {
      dispatch({ type: SET_STORAGE_LIST, data });
    },
    setFileType(data) {
      volumeByType(data).then((res) => {
        const { data } = res;
        dispatch({ type: SET_FILE_TYPE, data });
      });
    },
    setStorageFormat(data) {
      countsByFormat(data).then((res) => {
        const { data } = res;
        dispatch({ type: SET_STORAGE_FORMAT, data });
      });
    },
    setRetentionPeriod(data) {
      volumeByPeriod(data).then((res) => {
        const { data } = res;
        dispatch({ type: SET_PERIOD, data });
      });
    },
    setCategory(data) {
      volumeByCategory(data).then((res) => {
        const { data } = res;
        dispatch({ type: SET_CATEGORY, data });
      });
    },
    setSecret(data) {
      volumeBySecret(data).then((res) => {
        const { data } = res;
        dispatch({ type: SET_SECRET, data });
      });
    },
    setTime(data) {
      volumeByTime(data).then((res) => {
        const { data } = res;
        dispatch({ type: SET_TIME, data });
      });
    },
    setUnit(data) {
      volumeByUnit(data).then((res) => {
        const { data } = res;
        dispatch({ type: SET_UNIT, data });
      });
    },
    setPermiss(data) {
      volumeByPermiss(data).then((res) => {
        const { data } = res;
        dispatch({ type: SET_PERMISS, data });
      });
    }
  };
  useEffect(() => {}, []);
  return (
    <FamilyStatisticsContext.Provider value={{ ...state, ...actions }}>
      {props.children}
    </FamilyStatisticsContext.Provider>
  );
};
export const useFamilyStatisticsContext = () => {
  return useContext(FamilyStatisticsContext);
};
export { FamilyStatisticsContext, FamilyStatisticsContextProvider };
